<?php
$caption = BUtils::T('specials_nearby') . ' - Yamii.fi';
$this->pageTitle = $caption;

$ios_url = Yii::app()->createUrl('app/mobile', array('download'=>1, 'type'=>'iphone'));
$ios_image_link = Yii::app()->baseUrl . '/img/app-store-icon.png';
$android_url = Yii::app()->createUrl('app/mobile', array('download'=>1, 'type'=>'android'));
$android_image_link = Yii::app()->baseUrl . '/img/google-play-icon.png';
$windows_url = Yii::app()->createUrl('/mobile/default/windows');
$windows_image_link = Yii::app()->baseUrl . '/img/windows-store-icon-coming.png';
$download_box = '';
$get_yamii_app_btn = BUtils::T('get_yamii_app_btn');
$detect = new Mobile_Detect();
if ($detect->isIOS()){
	$download_box = <<<HTML
<div style="float:left; margin-right:8px;">
	<a href="{$ios_url}" target="_blank"><img style="height:32px;" src="{$ios_image_link}" alg="" /></a>
</div>
<div style="font-size:12px;">
	<a href="{$ios_url}" class="btn btn-info" target="_blank">{$get_yamii_app_btn}</a>
</div>
HTML;
} elseif ($detect->isAndroidOS()) {
	$download_box = <<<HTML
<div style="float:left; margin-right:8px;">
	<a href="{$android_url}" target="_blank"><img style="height:32px;" src="{$android_image_link}" alg="" /></a>
</div>
<div style="font-size:12px;">
	<a href="{$android_url}" class="btn btn-info" target="_blank">{$get_yamii_app_btn}</a>
</div>
HTML;
} elseif ($detect->isWindowsPhoneOS()) {
	$download_box = <<<HTML
<div style="float:left; margin-right:8px;">
	<a href="{$windows_url}"><img style="height:32px;" src="{$windows_image_link}" alg="" /></a>
</div>
<div style="font-size:12px;">
	<a href="{$windows_url}" class="btn btn-info" target="_blank">{$get_yamii_app_btn}</a>
</div>
HTML;
} else {
	$download_box = <<<HTML
<a href="{$ios_url}" target="_blank"><img style="height:32px;" src="{$ios_image_link}" alg="" /></a>
<a href="{$android_url}" target="_blank"><img style="height:32px;" src="{$android_image_link}" alg="" /></a>
<a href="{$windows_url}"><img style="height:32px;" src="{$windows_image_link}" alg="" /></a>
HTML;
}
?>
<div class="row-fluid">
	<div class="span12" style="margin-top:15px;">
		<div class="box_white relative">
			<div class="clearfix" style="margin:8px 0 8px 8px; text-align:center;">
				<?php echo $download_box; ?>
			</div>
		</div>
		<div class="box_white relative">
			<div class="pull-right" style="margin:8px 0 0 0;">
				<a class="btn locate-me"><?php echo BUtils::T('locate_me_btn'); ?></a>
			</div>
			<div class="title-bar pull-left">
				<h4 class="blue"><?php echo BUtils::T('specials_nearby'); ?></h4>
			</div>
			<div class="clearfix"></div>
			<div id="tips-no-location" style="display:none; text-align:center">
				<span class="label label-inverse"><?php echo BUtils::T('location_deny_msg'); ?></span>
			</div>
			<div id="loading-data"></div>
		</div>
		<div class="box_white relative">
			<div id="loading-bar" style="text-align:center; padding:8px 0;">
				<img src="<?php echo Yii::app()->baseUrl; ?>/img/loader-bar.gif" style="max-width:100%;" />
				<a href="javascript:;" style="display:none;"><?php echo BUtils::T('offer_more_lbl'); ?></a>
				<span style="display:none;">No more offer.</span>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
var g_position = {};
$(function() {
	getLocation();
	$('.locate-me').click(function () {
		getLocation();
	});
	$('#loading-bar a').click(function() {
		if (g_position.offset) g_position.offset += 25;
		else g_position.offset = 25;
		loadOffers(g_position, false);
	});
});

function getLocation() {
	if (navigator.geolocation) {
		var options = {timeout:4000, maximumAge:300000};
		navigator.geolocation.getCurrentPosition(successLocate, errorLocate, options);
	} else {
		loadOffers(null, true);//use the default position
	}
}

function successLocate(position) {
	//loading the offers based on the current position
	g_position = {lat: position.coords.latitude, lng: position.coords.longitude};
	loadOffers(g_position, true);
}

function errorLocate(error) {
	var message = 'undefined error.';
	switch (error.code) {
		case error.PERMISSION_DENIED:
			message = "User denied the request for Geolocation."
		break;
		case error.POSITION_UNAVAILABLE:
			message = "Location information is unavailable."
		break;
		case error.TIMEOUT:
			message = "The request to get user location timed out."
		break;
		case error.UNKNOWN_ERROR:
			message = "An unknown error occurred."
		break;
	}
	_push({msg: message, func: 'getLocation'});
	//use the default position
	loadOffers(null, true);
}

function loadOffers(data, isNew) {
	if (null == data) $('#tips-no-location').show();
	else $('#tips-no-location').hide();
	$.ajax({
		url: '<?php echo Yii::app()->createUrl($this->module->id . '/' . $this->id . '/offers'); ?>',
		data: data,
		beforeSend: function() {
			if (isNew) $('#loading-data').html('');
			$('#loading-bar span').hide();
			$('#loading-bar a').hide();
			$('#loading-bar img').show();
		},
		success: function(data) {
			$('#loading-bar img').hide();
			if (isNew) $('#loading-data').html(data);
			else $('#loading-data').append(data);
			
			if (data == '') { $('#loading-bar a').hide(); $('#loading-bar span').show(); }
			else { $('#loading-bar a').show(); $('#loading-bar span').hide(); }
		}
	});
}
</script>